package com.oracle.jimu.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.oracle.jimu.bean.Attr;
import com.oracle.jimu.bean.ProductAttr;

public class AttrDaoImpl implements AttrDao {
	private SessionFactory sessionFactory;

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Attr> attrList() {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		String hql="from com.oracle.jimu.bean.Attr";
		Query q=session.createQuery(hql);
		List list = q.list();		
		System.out.println("list.size()="+list);
		
		return list;
	}

	@Override
	public void addAttr(Attr attr) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		session.save(attr);
	}

	@Override
	public void deleteAttr(Integer attrid) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Attr a = (Attr) session.get(Attr.class, attrid);
		a.getSubType().setAttrs(null);
		a.getProductAttr().getProduct().setProductAttrs(null);
		session.delete(a);
	}

	@Override
	public void addProdAttr(ProductAttr prodAttr) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		session.save(prodAttr);	
	}

	@Override
	public void deleteProdAttr(Integer id) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		ProductAttr pa=(ProductAttr) session.get(ProductAttr.class, id);
		pa.getProduct().setProductAttrs(null);
		session.delete(pa);
	}


	
}
